package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.model.GongYingListModel;

import java.util.ArrayList;
import java.util.List;


public class GongYingShangIndexAdapter extends RecyclerView.Adapter<GongYingShangIndexAdapter.ViewHolder> {
    List<GongYingListModel.DataBean> myLiveList = new ArrayList<>();
    Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition = -1;

    //recycleview标签
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<GongYingListModel.DataBean> myLiveList);
    }


    public GongYingShangIndexAdapter(Context context, List<GongYingListModel.DataBean> myLiveList) {
        this.mContext = context;
        this.myLiveList = myLiveList;

    }

    public void setDataList(List<GongYingListModel.DataBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gongyingindex, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.commodityName.setText(myLiveList.get(position).getSupplierName());
        holder.text_LeiBie.setText(myLiveList.get(position).getCategory());
        if (myLiveList.get(position).getQuantity()==0) {
            holder.check_shop.setChecked(false);
            holder.btn_yes_no.setText("否");
        } else {
            holder.check_shop.setChecked(true);
            holder.btn_yes_no.setText("是");
        }

        holder.check_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });

    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView commodityName;
        LinearLayout content;
        TextView text_LeiBie;
        CheckBox check_shop;
Button btn_yes_no;
        public ViewHolder(View itemView) {
            super(itemView);

            commodityName = (TextView) itemView.findViewById(R.id.commodityName);
            content = itemView.findViewById(R.id.content);
            text_LeiBie = itemView.findViewById(R.id.text_LeiBie);
            check_shop = itemView.findViewById(R.id.check_shop);
            btn_yes_no = itemView.findViewById(R.id.btn_yes_no);
        }
    }


}
